import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/publicBussine/loginInfo/bussine_loginExit.dart';
import 'package:honeypotflutter/public/public_total.dart';
import '../../api/login_api.dart';
import '../../model/login_param.dart';

class LoginForgetLogic extends GetxController {
  final TextEditingController phoneController = TextEditingController();
  final TextEditingController codeController = TextEditingController();
  final TextEditingController pwdController = TextEditingController();
  final TextEditingController againPwdController = TextEditingController();

  /// 是否可点击
  RxBool isEnable = RxBool(false);

  /// 参数
  LoginParam param = LoginParam();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    phoneController.addListener(() {
      param.phoneNumber = phoneController.text;
      _changeButtonAction();
    });
    codeController.addListener(() {
      param.code = codeController.text;
      _changeButtonAction();
    });
    pwdController.addListener(() {
      param.phoneNumber = pwdController.text;
      _changeButtonAction();
    });
    againPwdController.addListener(() {
      _changeButtonAction();
    });
  }

  /*
  * 修改
  * */
  _changeButtonAction() {
    isEnable.value = phoneController.text.isNotEmpty && codeController.text.isNotEmpty && pwdController.text.isNotEmpty && againPwdController.text.isNotEmpty;
  }

  /*
  * 判断手机号
  * */
  bool isPhoneNumber() {
    if (!GetUtils.isPhoneNumber(phoneController.text)) {
      HLTotal.total("请输入正确手机号码");
      return false;
    }
    return true;
  }

  /*
  * 短信
  * */
  sendMessage() async {
    String phone = phoneController.text;
    LoginApi.sendMessage(phone, 1).then((value) {
      param.codeToken = value;
    });
  }

  /*
  * 忘记密码提交
  * */
  forgetClick() async {
    if (!isPhoneNumber()) {
      return;
    }
    if (param.codeToken == null || param.codeToken!.isEmpty) {
      HLTotal.total("请获取验证码");
      return;
    }
    String pwd = pwdController.text;
    String againPwd = againPwdController.text;
    if (pwd.length < 6) {
      HLTotal.total("密码设置最少6位");
      return;
    }

    if (GetUtils.isNumericOnly(pwd) || GetUtils.isAlphabetOnly(pwd)) {
      HLTotal.total("请按要求设置密码");
      return;
    }

    if (pwd != againPwd) {
      HLTotal.total("两次输入密码不相同");
      return;
    }

    LoginApi.forget(param).then((value) {
      HLTotal.total("修改成功");
      Map<String, bool> arguments = Get.arguments;
      if (arguments["sourceSetting"] != null && arguments["sourceSetting"]! == true) {
        LoginExit.quit();
      } else {
        HLGet.back();
      }
    });
  }
}
